﻿using SetMenu.Helper;
using SetMenu.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SetMenu
{
    class Program
    {
        static string appId = "wx32664f27282048b7";
        
        static string appSecret = "7994686235e955ce26915dca06f05770";
        static HttpHelper help = new HttpHelper();
        static void Main(string[] args)
        {
            string miniprogramAppId = "wx404eddb6dea0d0c0";
            string token = GetToken();
            Menu m =GetMens(token);
            //Button b = FindButton(m, "专家咨询");
            //b.sub_button.Remove(b.sub_button[3]);


            //miniprogram mini = new miniprogram();
            //mini.name = "孕育视频";
            //mini.type = "miniprogram";
            // mini.url = "http://mp.weixin.qq.com";
            //mini.appid = miniprogramAppId;
            //mini.pagepath = "pages/xi";
            //b.sub_button.Add(mini);
            //设置专家咨询
            //m.menu.button[1].sub_button = new List<Sub_button>();
            //m.menu.button[1].name = "专家咨询";
            //m.menu.button[1].type = "miniprogram";
            //m.menu.button[1].url = "http://mp.weixin.qq.com";
            //m.menu.button[1].appid = miniprogramAppId;
            //m.menu.button[1].pagepath = "pages/yan";

            //设置个人中心

            //Button b = m.menu.button[2];
            //miniprogram mini = new miniprogram();
            //mini.name = "注册";
            //mini.type = "miniprogram";
            //mini.url = "http://mp.weixin.qq.com";
            //mini.appid = miniprogramAppId;
            //mini.pagepath = "pages/personalSettings";
            //mini.sub_button = new List<Sub_button>();
            //b.sub_button.Add(mini);


            //miniprogram mini2 = new miniprogram();
            //mini2.name = "我的";
            //mini2.type = "miniprogram";
            //mini2.url = "http://mp.weixin.qq.com";
            //mini2.appid = miniprogramAppId;
            //mini2.pagepath = "pages/bang";
            //mini2.sub_button = new List<Sub_button>();
            //b.sub_button.Add(mini2);

            CreateButtonModel cbm = new CreateButtonModel();
            cbm.button = m.menu.button;
            string rs =SetButton(cbm,token);
            Console.WriteLine(rs);
            Console.WriteLine("End .."); 
            Console.ReadKey();
        }





        /// <summary>
        /// 修改公众号子菜单url值
        /// </summary>
        private void SetSubButtonUrl()
        {
            string token = GetToken();
            Menu m = GetMens(token);
            CreateButtonModel b = new CreateButtonModel();
            b.button = m.menu.button;
            string rs = SetButton(b, token);
            Console.WriteLine(rs);
            Console.ReadKey();
        }

        private static Button FindButton(Menu m, string btnName)
        {
            var btnOne = m.menu.button.FirstOrDefault(x => x.name == btnName);
            return btnOne;
        }

        private static Sub_button FindSubButon(Menu m,string btnName,string subBtnName)
        {
            var btnOne = m.menu.button.FirstOrDefault(x => x.name == btnName);
            var subBtn = btnOne.sub_button.FirstOrDefault(x => x.name == subBtnName);
            return subBtn;
        }


        public static string SetButton(CreateButtonModel b,string token)
        {
            string createUrl = string.Format("https://api.weixin.qq.com/cgi-bin/menu/create?access_token={0}", token);
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(b);
            string ContentType = "application/json";
            string rs = help.HttpPostData(createUrl, json, ContentType);
            return rs;
        }


        private static Menu GetMens(string token)
        {
            string url = string.Format("https://api.weixin.qq.com/cgi-bin/menu/get?access_token={0}", token);
            string menusJson = help.HttpGetData(url);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<Menu>(menusJson);
        }

        private static string GetToken()
        {
            string tokenUrl = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appId, appSecret);
            string tokenJson = help.HttpGetData(tokenUrl);
            dynamic tokenObj = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(tokenJson);
            string token = tokenObj.access_token;
            return token;
        }

    }
}
